//
//  RegisterViewController.m
//  FindJob
//
//  Created by 233 on 16/2/29.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "RegisterViewController.h"
#import "LoginViewController.h"
#import "RegisterAPIManger.h"
#import "UserModel.h"

static NSString *const kLoginViewController = @"LoginViewController";
@interface RegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UITextField *confirmPasswordTF;
@property(nonatomic,strong) RegisterAPIManger *registerAPIManger;

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
}
#pragma mark *** registerBtn ***

- (IBAction)registerBtn:(UIButton *)sender {
    
    if ([self judgeEmailTextFeildIsValid:self.accountTF] || [self judgePhoneTextFeildIsValid:self.accountTF]){
        
        NSDictionary *parameter = @{@"userName":self.accountTF.text,
                                    @"password":self.passwordTF.text};
        
        __weak RegisterViewController *myself = self;
        [self.registerAPIManger startRegisterRequestParameter:parameter responds:^(RespondsStatus status, NSString *message) {
            if (status == RespondsStatusDataError ) {
              
                if (![self.passwordTF.text isEqualToString:self.confirmPasswordTF.text]) {
                    
                      //弹框提示
                    [self showAlertViewControllerWithTarget:self title:@"温馨提示" message:@"两次输入密码不一致"];
                }
               [self showAlertViewControllerWithTarget:self title:@"温馨提示" message:@"请输入正确的手机号和邮箱"];
            
            }else{
                if (status == RespondsStatusSuccess) {
                    
                    UserModel *userModel = [UserModel shareUserModel];
                    userModel.isRegisterSuccess = YES;
                    userModel.account = self.accountTF.text;
                    userModel.password = self.passwordTF.text;
                    [userModel saveAccountAndPassword];
                    [myself.navigationController popViewControllerAnimated:YES];
                }
                
            }
            NSLog(@"message = %@",message);
        }];
    }else{
        return;
    }
    
}
#pragma mark ***  ***
// 执行联线的时候调用
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.destinationViewController isKindOfClass:[LoginViewController class]]) {
        LoginViewController *loginVc = segue.destinationViewController;
        
    }
}
#pragma mark *** private ***
- (void)showAlertViewControllerWithTarget:(id _Nonnull)target title:(NSString *_Nonnull)title message:(NSString *_Nonnull)message{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
    [target presentViewController:alertController animated:YES completion:nil];
}

- (IBAction)unwindToMainViewController:(UIStoryboardSegue *)unwindSegue{
    
}
#pragma mark *** getters *** 
- (BOOL)judgeEmailTextFeildIsValid:(UITextField *)textField{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:textField.text];
    
}
- (BOOL)judgePhoneTextFeildIsValid:(UITextField *)textField{
    
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:textField.text];
}
#pragma mark *** getters ***
- (RegisterAPIManger *)registerAPIManger{
    if (!_registerAPIManger) {
        _registerAPIManger = [[RegisterAPIManger alloc] init];
    }
    return _registerAPIManger;
}
@end
